#ifndef __ARM_DEV_SDK_H__
#define __ARM_DEV_SDK_H__

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stddef.h>
#include <malloc.h>

typedef void            VOID;
typedef char            CHR;
typedef char            UINT1;
typedef short           UINT2;
typedef int             INT4;
typedef unsigned int    UINT4;

#define CMD_STR_WORDS   8
#define CMD_WORD_LEN    64
#define CMD_LENTH       128
#define SDK_CMD_LEVEL   8

typedef char CMD_ARRAY[CMD_STR_WORDS][CMD_WORD_LEN];
typedef char* CMDS_ADDR[CMD_STR_WORDS];

typedef enum {
    FALSE = 0,
    TRUE
} BOOL;

typedef enum {
    CMD_LEVEL_ROOT = 0,
    CMD_LEVEL_MODULE,
    CMD_LEVEL_FUNCTION,
    CMD_LEVEL_END
} CMDLEVEL;

typedef struct cmdStrList{
    CMD_ARRAY au1CmdStrPtr;
    struct cmdStrList *next;
} cmdStrList_t;

typedef struct cmdHistList{
    CHR au1CmdHistStr[CMD_LENTH];
    struct cmdHistList *prev;
    struct cmdHistList *next;
} cmdHistList_t;

#define STDIN   stdin
#define STDOUT  stdout
#define STDERR  stderr

#define PRINTF   printf
#define SPRINTF  sprintf
#define SNPRINTF snprintf
#define STRCPY   strcpy
#define STRCMP   strcmp
#define STRNCMP  strncmp
#define MEMCPY   memcpy
#define MEMSET   memset
#define MEMCMP   memcmp

// 调试模式开关
#define DEBUG_MODE 0

#if DEBUG_MODE
    #define ARM_SDK_DEBUG(fmt, ...) printf("[%s:%d] " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
#else
    #define ARM_SDK_DEBUG   // 发布模式下为空，完全移除日志
#endif

#ifndef GLOB_VAR
#define GLOB_VAR
extern CHR *pu1Prompt[SDK_CMD_LEVEL];
extern CMDLEVEL eCurCmdLevel;
extern cmdStrList_t gu1CmdList;
extern cmdHistList_t gcmdHistHead;
extern int (*armSdkCmdFuncs[])(int, char const **);
#endif

#define ARM_DEV_SDK_INFO \
    "ARM-v7 research and development for\n" \
    "****************************************************\n" \
    "* gpio: *\n" \
    "* ccm: *\n" \
    "* platform: *\n" \
    "* iic: *\n" \
    "* spi: *\n" \
    "* ieee802.3: *\n" \
    "* wifi: *\n" \
    "* bluetooth: *\n" \
    "****************************************************\n"

#define ARM_CMD_PROMPT()  \
{ \
    if (pu1Prompt[eCurCmdLevel] == NULL) \
    { \
        PRINTF("# "); \
    } \
    else \
    { \
        PRINTF("(%s)# ", pu1Prompt[eCurCmdLevel]); \
    } \
}

#define ARM_CMD_PROMPT_QUIT()  \
{ \
    free(pu1Prompt[eCurCmdLevel]); \
    pu1Prompt[eCurCmdLevel] = NULL; \
    eCurCmdLevel --; \
}

#define ARM_CMD_PROMPT_ADD(str)  \
{ \
    eCurCmdLevel ++; \
    pu1Prompt[eCurCmdLevel] = (CHR *)malloc(CMD_WORD_LEN); \
    if (pu1Prompt[eCurCmdLevel] == NULL) \
    { \
        MEM_LLOC_ERR(FAILURE); \
    } \
    MEMSET(pu1Prompt[eCurCmdLevel], 0, CMD_WORD_LEN); \
    MEMCPY(pu1Prompt[eCurCmdLevel], (str), strlen(str)); \
}

#define ARM_CMD_IS_SUBMODULE(str) if(MEMCMP(pu1Prompt[CMD_LEVEL_MODULE], str, strlen(pu1Prompt[CMD_LEVEL_MODULE])) == 0)
#define ARM_CMD_IS_FUNCTION(str) if(MEMCMP(pu1Prompt[CMD_LEVEL_FUNCTION], str, strlen(pu1Prompt[CMD_LEVEL_FUNCTION])) == 0)

#define ARM_CMD_INPUT(c, str, cnt) adAPISdkCmdInputHandle(c, str, cnt)

#endif // __ARM_DEV_SDK_H__

